/*
 * $Id: TaskTypeDAO.java,v 1.13 2009/07/29 17:22:59 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.database.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springmodules.cache.annotations.Cacheable;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestDefn;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskRelationship;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskType;
import com.ge.healthcare.autosc.common.exceptions.FaultRecorderException;

public class TaskTypeDAO extends BaseDAO implements ITaskTypeDAO, Serializable {

	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -8595838729734330888L;
	private ICodeDetailsDAO codeDetailsDAO = null;
	private ITaskDAO taskDAO = null;

	public TaskTypeDAO() {
		super(CfgTaskType.TABLE_NAME);
	}

	@Cacheable(modelId = "taskTypeDAOCacheModel")
	public ArrayList<CfgTaskType> getNextTaskTypes(CfgTaskType taskType) {
		StringBuffer query = new StringBuffer("select taskType from ");
		query.append(CfgTaskRelationship.TABLE_NAME).append(" as taskRel, ");
		query.append(CfgTaskType.TABLE_NAME).append(" as taskType ");
		query.append(" where ");
		query.append(" taskType.").append(CfgTaskType.COL_ID).append(" = ");
		query.append(" taskRel.").append(CfgTaskRelationship.COL_CHILD_ID);
		query.append(" and ");
		query.append(" taskRel.").append(CfgTaskRelationship.COL_PARENT_ID);
		query.append(" = ?");

		ASCLogger.debug(this.getClass(), "getNextTaskTypes", new StringBuffer()
				.append("Ruuning where clause using query ").append(query)
				.append(" for taskType: " + taskType));
		List<?> values = getHibernateTemplate().find(query.toString(),
				taskType.getTaskTypeId());
		ArrayList<CfgTaskType> tasks = new ArrayList<CfgTaskType>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				tasks.add((CfgTaskType) values.get(i));
			}
		}
		return tasks;
	}

	@Cacheable(modelId = "taskTypeDAOCacheModel")
	public CfgTaskType getByID(Long id) {
		Object requestType = findObjectByID(CfgTaskType.COL_ID, id);
		if (!ASCUtil.isNullOrEmptyObject(requestType)) {
			return (CfgTaskType) requestType;
		}
		return null;
	}

	@Cacheable(modelId = "taskTypeDAOCacheModel")
	public CfgTaskType getByName(String name) {
		if (ASCUtil.isNullOrEmpty(name)) {
			throw new FaultRecorderException("TaskType Name passed was Null");
		}
		Object requestType = findObjectByID(CfgTaskType.COL_NAME, name);
		if (!ASCUtil.isNullOrEmptyObject(requestType)) {
			return (CfgTaskType) requestType;
		}
		return null;
	}

	@Cacheable(modelId = "taskTypeDAOCacheModel")
	public ArrayList<ActTask> getChildTasks(ActTask task, CfgTaskType taskType,
			String codeName) {
		String methodName = "getChildTasks";
		ASCLogger.debug(this.getClass(), methodName, "For Task: " + task
				+ " TaskType: " + taskType);
		ArrayList<ActTask> nextTasks = new ArrayList<ActTask>();
		// Making the assumption that Task only needs request Id
		ActRequest request = new ActRequest();
		request.setRequestId(task.getRequestId());
		List<CfgTaskType> retTasks = this.getNextTaskTypes(taskType);
		for (Iterator<CfgTaskType> iterator = retTasks.iterator(); iterator
				.hasNext();) {
			CfgTaskType newTaskType = iterator.next();
			ASCLogger.debug(this.getClass(), methodName, "Child TaskType: "
					+ newTaskType);
			if (codeDetailsDAO.isChildTaskType(task, newTaskType)) {
				ASCLogger.debug(this.getClass(), methodName,
						"Child Task: Added");
				nextTasks.add(taskDAO.initActTask(request, newTaskType, task,
						codeName));
			}
		}
		return nextTasks;
	}

	@Cacheable(modelId = "taskTypeDAOCacheModel")
	public CfgTaskType getTaskByID(CfgRequestDefn requestDefn) {
		return getByID(requestDefn.getInitialTaskId());
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	/**
	 * @return the codeDetailsDAO
	 */
	public ICodeDetailsDAO getCodeDetailsDAO() {
		return codeDetailsDAO;
	}

	/**
	 * @param codeDetailsDAO
	 *            the codeDetailsDAO to set
	 */
	public void setCodeDetailsDAO(ICodeDetailsDAO codeDetailsDAO) {
		this.codeDetailsDAO = codeDetailsDAO;
	}

	/**
	 * @return the taskDAO
	 */
	public ITaskDAO getTaskDAO() {
		return taskDAO;
	}

	/**
	 * @param taskDAO
	 *            the taskDAO to set
	 */
	public void setTaskDAO(ITaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}

}
